﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MCDirectBroadcast.Entity;
using System.Threading;

namespace MCDirectBroadcast
{
    public class MCDierectServer
    {
        private Dictionary<string, StateObject> _members;
		private AsynchronousSocketListener _sockListener;

        public List<MemberObject> Members
        {
            get {
                if (_members == null)
                    return new List<MemberObject>();

                List<MemberObject> ltemp = new List<MemberObject>();

                foreach (var key in _members.Keys)
                {
                    ltemp.Add(new MemberObject()
                    {
                        UUID = key,
                        Name = _members[key].UUID,
                        IPAddress = ""
                    });
                }
                return ltemp;
            }
        }

		public MCDierectServer(int port)
		{
			_sockListener = new AsynchronousSocketListener(port);
		}

        
        #region Events

        protected virtual void OnMemberSignIn(MemberSignEventArgs e)
        {
            MemberSignIn(this, e);
        }

        protected virtual void OnStorySend(StorySendEventArgs e)
        {
            StorySent(this, e);
        }

        protected virtual void OnHostLive(HostActionEventArgs e)
        {
            HostOn(this, e);
        }

        protected virtual void OnHostOff(HostActionEventArgs e)
        {
            HostOff(this, e);
        }

        public event MemberSignInHandler MemberSignIn;

        public event StorySentHandler StorySent;

        public event HostActionHandler HostOff;

        public event HostActionHandler HostOn;

        #endregion

        #region Public stuffs

		public void StartHosting()
		{
			Thread t = new Thread(new ThreadStart(this._sockListener.Start));
			t.Start();

			HostOn(this, new HostActionEventArgs() { });
		}

		public void StopHosting()
		{
			this._sockListener.Stop();
			HostOff(this, new HostActionEventArgs() { });
		}

        public int SendMessage(string UUID, string message)
        {
            return -1;
        }
        
        #endregion
    }


    public class MemberSignEventArgs : EventArgs
    { 
    
    }

    public delegate void MemberSignInHandler(object sender, MemberSignEventArgs e);

    public class StorySendEventArgs : EventArgs
    {

    }

    public delegate void StorySentHandler(object sender, StorySendEventArgs e);

    public class HostActionEventArgs : EventArgs
    {

    }

    public delegate void HostActionHandler(object sender, HostActionEventArgs e);
}
